Ext.define('extjs6test.view.base.BaseEditWindowController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.baseew',
    onCloseButtonClick: function (button, evt) {
        this.getView().close();
    },
    onOkButtonClick: function (button, evt) {
        var win = this.getView();
        var formStore = win.formStore;
        var form = win.down('form');
        if (form.isValid()) {
            form.updateRecord();
            var record = form.getRecord();
            if(win.mode == "new")
                formStore.add(record);
            formStore.sync({
                success:function (batch,opts) {
                    if(!isContinue){
                        win.close();
                    }
                    else{
                        var record = Ext.create(formStore.model);
                        record.data.id = null;
                        form.loadRecord(record);
                        form.reset();
                        isContinue.setValue(true);
                    }
                },
                failure:function (batch, opts) {
                    console.log(batch);
                    console.log(opts);
                    Ext.Msg.alert("错误","参数格式错误");
                }
            });

            var isContinue=form.down('checkbox[name="isContinue"][value=true]');

        }
    },
    onWindowRender:function (win) {

    },
    onWindowBeforeRender: function (win) {

    },
    onWindowAfterRender: function (win) {
        var ctl = this;

        if (win.mode == "new") {
            ctl.onNewModeAfterRender(win);
        }
        else if (win.mode == "modify") {
            ctl.onModifyModeAfterRender(win);
        }
        else if (win.mode == "detail") {
            ctl.onDetailModeAfterRender(win);
        }

        if (win.formRecord) {
            win.down('form').loadRecord(win.formRecord);
        }
    },
    onNewModeAfterRender: function (win) { // mode = new  called by afterrender
        var hideFields = ["id", "isDeleted", "joinTime", "updateTime"];
        Ext.each(hideFields, function (name, idx) {
            var field = win.down("[name=\"" + name + "\"]");
            field.hide();
            field.allowBlank = true;
        });
    },
    onModifyModeAfterRender: function (win) { //mode = modify  called by afterrender
        // var hideFields = ["isDeleted", "id"];
        var hideFields = [];
        Ext.each(hideFields, function (name, idx) {
            var field = win.down("[name=\"" + name + "\"]");
            field.hide();
            field.allowBlank = true;
        });
        win.down("checkbox[name=isContinue]").hide();
    },
    onDetailModeAfterRender: function (win) { //mode = detail called by afterrender
        var hideFields = ["isDeleted", "id"];
        Ext.each(hideFields, function (name, idx) {
            var field = win.down("[name=\"" + name + "\"]");
            field.allowBlank = true;
            field.hide();

        });
        var allFields = win.query("field");
        Ext.each(allFields, function (field, idx) {
            field.setReadOnly(true);
        });

        win.down('button[action="enter"]').hide();
        win.down("checkbox[name=isContinue]").hide();
    }
});